require 'fileutils'

dir = Dir.new("/mnt/f/ruby_project/my_api/public/upload/项目总体计划及子计划/dev")
puts dir.children.length
puts dir.path
pwd = dir.path
dir.children.each do |file|
  puts file
  puts File.stat(pwd + '/' + file).mtime.strftime('%Y-%m-%d %H:%M:%S') # 最后一次修改文件或者目录（指文件内容被修改）的时间
  puts File.stat(pwd + '/' + file).ctime # 最后一次改变文件或者目录（改变的是元数据属相，比如文件名、内容，大小，权限，所属组等）的时间
  puts File.stat(pwd + '/' + file).atime # 最近的访问时间 access time
end

sortFiles = dir.children.sort { |a, b| File.stat(pwd + '/' + b).mtime <=> File.stat(pwd + '/' + a).mtime }
sortFiles.each do |file|
  puts file # 安照mtime逆序排序，即最新修改的在前
end

# cpFile = sortFiles.first
# unless  Dir.exist?(pwd+'/../release')
#   Dir.mkdir(pwd+'/../release')
# end
# FileUtils.cp(pwd+'/'+cpFile, pwd+'/../release')

my_str = "ceshiewndang.txt"
puts my_str.rindex('.')
idx = my_str.rindex('.')
puts my_str[...idx]

def get_same_file_num(filename, parent_path)
  parent_dir = Dir.new(parent_path)
  name = filename[...filename.rindex('.')]
  file_list = parent_dir.children
  num = 0
  file_list.each do |file|
    file = file[...file.rindex('.')]
    if !file.nil? && (file.include? name)
      num += 1
    end
  end
  num
end

def get_same_file_list(filename, parent_path)
  parent_dir = Dir.new(parent_path)
  name = filename[...filename.rindex('.')]
  file_list = parent_dir.children
  same_file_list = []
  file_list.each do |file|
    file = file[...file.rindex('.')]
    if !file.nil? && (file.include? name)
      same_file_list << file
    end
  end
  same_file_list
end

def rename_file(filename, rename, parent_path)
  file_path = parent_path + filename
  new_file_path = parent_path + rename
  if File.exist?(file_path)
    File.rename(file_path, new_file_path)
  end
end

def get_next_num(file_list)
  temp = []
  file_list.each do |file|
    puts file
    if file.rindex('(') && file.rindex(')')
      num = file[file.rindex('(') + 1...file.rindex(')')]
    end
    temp << num.to_i if num
    temp.sort!
  end
  next_num = file_list.length
  (1...file_list.length).each { |i|
    if i != temp[i - 1]
      next_num = i
      break # 补全第一个丢失的数字
    end
  }
  next_num
end

def copy_in_place(filename, parent_path)
  file_path = parent_path + filename
  suffix = filename[filename.rindex('.')...]
  new_filename = filename[...filename.rindex('.')] + " - 副本" + suffix
  num = get_same_file_num(new_filename, parent_path)
  same_file_list = get_same_file_list(new_filename, parent_path)

  if num > 0
    next_num = get_next_num(same_file_list)
    cp_file_path = parent_path + new_filename[...new_filename.rindex('.')] + " (#{next_num})" + suffix
  else
    cp_file_path = parent_path + new_filename[...new_filename.rindex('.')] + suffix
  end
  if File.exist?(file_path)
    FileUtils.cp(file_path, cp_file_path)
  end
end

puts File.exist?("/mnt/f/ruby_project/my_api/public/upload/项目总体计划及子计划/dev/v1_01 感控一体化助力单元研制项目总体计划-v0.91.xlsx")
p rename_file("v1_01 123感控一体化助力单元研制项目总体计划-v0.91.xlsx", "v1_01 感控一体化助力单元研制项目总体计划-v0.91.xlsx", "/mnt/f/ruby_project/my_api/public/upload/项目总体计划及子计划/dev/")
puts get_same_file_num("v1_01 感控一体化助力单元研制项目总体计划-v0.91.xlsx", "/mnt/f/ruby_project/my_api/public/upload/项目总体计划及子计划/dev/")
p copy_in_place("v1_01 感控一体化助力单元研制项目总体计划-v0.91.xlsx", "/mnt/f/ruby_project/my_api/public/upload/项目总体计划及子计划/dev/")
